package com.yun.algorithmproblem.leetcode;

import java.util.ArrayList;
import java.util.List;

public class Leetcode3159 {

    public static void main(String[] args) {
        Leetcode3159 obj=new Leetcode3159();
        int[] nums=new int[]{1,3,1,7};
        int[] queries=new int[]{1,1,2,4};
        int x=1;
        obj.occurrencesOfElement(nums,queries,x);
    }

    public int[] occurrencesOfElement(int[] nums, int[] queries, int x) {
        List<Integer> indices = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == x) {
                indices.add(i);
            }
        }

        int[] res = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {
            if (indices.size() < queries[i]) {
                res[i] = -1;
            } else {
                res[i] = indices.get(queries[i] - 1);
            }
        }
        return res;
    }
}
